package opdracht1.client;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.io.*;

public class Database {

	public static void main(String[] args) {

		// Laad eerst de driver.
		try {
			Class.forName("com.mysql.jdbc.Driver");
			System.out.println("Laden van de driver is gelukt.");
		} catch (ClassNotFoundException e) {
			System.err.println("Kan de driver niet in de classpath vinden.");
			e.printStackTrace();
			return;
		}

		// Voorbereid de connectie, statement, resultset, URL en SELECT query.
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		String url = "jdbc:mysql://localhost:3307/opdracht1?user=root&password=usbw";
		String selectQuery = "SELECT * FROM studenten";

		try {
			// Verkrijg de connectie.
			connection = DriverManager.getConnection(url);

			// Verkrijg de statement.
			statement = connection.createStatement();

			// Voer de SELECT query uit.
			resultSet = statement.executeQuery(selectQuery);

			// Verwerk de resultaten.
			while (resultSet.next()) {
				long id = resultSet.getLong("id");
				String naam = resultSet.getString("naam");
				System.out.println("ID=" + id + ",Naam=" + naam);
				
				
				
			}
			connection.close();
			// Klaar!
			System.out.println("Uitvoeren van de SELECT query is gelukt.");
		} catch (SQLException e) {
			// Foutje?
			System.err.println("Uitvoeren van de SELECT query is mislukt.");
			
			e.printStackTrace();
		} 
		
	}

}

